package ext.generic.wfaugment;

import java.io.Serializable;

import java.util.ArrayList;

import wt.fc.ObjectIdentifier;
import wt.fc.Persistable;
import wt.fc.PersistenceHelper;

import wt.method.RemoteAccess;

import wt.org.OrganizationServicesHelper;
import wt.org.WTGroup;
import wt.org.WTOrganization;
import wt.org.WTPrincipal;
import wt.org.WTUser;

import wt.util.WTException;
import wt.util.WTProperties;

import com.ptc.core.components.forms.DynamicRefreshInfo;
import com.ptc.core.components.forms.FormProcessingStatus;
import com.ptc.core.components.forms.FormResult;
import com.ptc.core.components.forms.FormResultAction;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.netmarkets.util.misc.NmContext;

public class WorkFlowStepCommand implements RemoteAccess, Serializable
{
    private static boolean VERBOSE = false;
    public static String IBA_PRODUCT_RECORD_NUMBER = "ProductRecordsNumber";
    public static WTProperties wtProperties;
    static
    {
        try
        {
            wtProperties = WTProperties.getLocalProperties();
            VERBOSE = wtProperties.getProperty("ext.generic.workflowSteps.verbose", false);
        }
        catch (Throwable throwable)
        {
            throw new ExceptionInInitializerError(throwable);
        }
    }

    public WorkFlowStepCommand()
    {
    }

    public static FormResult addPrincipalsToParticipantSearch(NmCommandBean nmcommandbean) throws WTException
    {
        if (VERBOSE)
            System.out.println(" WorkFlowStepCommand addPrincipalsToParticipantSearch  begin");
        FormResult formresult = new FormResult(FormProcessingStatus.SUCCESS);
        formresult.setNextAction(FormResultAction.REFRESH_OPENER);
        String tableitem = nmcommandbean.getTextParameter("setParticipantsTableItemslist");
        if (tableitem == null)
            tableitem = "";
        if (VERBOSE)
            System.out.println(" WorkFlowStepCommand addPrincipalsToParticipantSearch  setParticipantsTableItemslist=" + tableitem);
        String s = nmcommandbean.getTextParameter("hiddenemail");
        if (VERBOSE)
            System.out.println(" WorkFlowStepCommand addPrincipalsToParticipantSearch hiddenemail=" + s);
        if (s == null)
        {
            formresult.setStatus(FormProcessingStatus.FAILURE);
            nmcommandbean.getDynamicOids();
            return formresult;
        }
        ArrayList arraylist = retrievePrincipalArrayFromPickerPrincipals(nmcommandbean, s);
        if (VERBOSE)
            System.out.println(" WorkFlowStepCommand addPrincipalsToParticipantSearch retrievePrincipalArrayFromPickerPrincipals =" + arraylist);
        for (int i = 0; i < arraylist.size(); i++)
        {
            WTPrincipal wtprincipal = getPrincipal((String) arraylist.get(i));
            if (wtprincipal == null)
            {
                continue;
            }
            String oid = PersistenceHelper.getObjectIdentifier(wtprincipal).toString();
            if (VERBOSE)
            {
                System.out.println(" WorkFlowStepCommand addPrincipalsToParticipantSearch  wtprincipal=" + wtprincipal.getDisplayIdentifier());
                System.out.println(" WorkFlowStepCommand addPrincipalsToParticipantSearch  wtprincipal oid=" + oid);
            }
            if (tableitem.indexOf(oid) == -1)
            {
                if (VERBOSE)
                    System.out.println(" WorkFlowStepCommand addPrincipalsToParticipantSearch  add to addDynamicRefreshInfo=" + wtprincipal.getDisplayIdentifier());
                formresult = addDynamicRefreshInfo(formresult, wtprincipal, "A");
            }
        }
        return formresult;

    }

    public static FormResult addPrincipalsToParticipantSelect(NmCommandBean nmcommandbean) throws WTException
    {
        if (VERBOSE)
            System.out.println(" WorkFlowStepCommand addPrincipalsToParticipantSelect  begin");
        FormResult formresult = new FormResult(FormProcessingStatus.SUCCESS);
        formresult.setNextAction(FormResultAction.REFRESH_OPENER);
        ArrayList arraylist = nmcommandbean.getSelected();
        String tableitem = nmcommandbean.getTextParameter("setParticipantsTableItemslist");
        if (tableitem == null)
            tableitem = "";
        if (VERBOSE)
        {
            System.out.println(" WorkFlowStepCommand addPrincipalsToParticipantSelect getSelected =" + arraylist);
            System.out.println(" WorkFlowStepCommand addPrincipalsToParticipantSelect  setParticipantsTableItemslist=" + tableitem);
        }
        for (int i = 0; i < arraylist.size(); i++)
        {
            NmContext nmcontext = (NmContext) arraylist.get(i);
            Persistable persistable = nmcontext.getTargetOid().getWtRef().getObject();
            if (!(persistable instanceof WTPrincipal))
                continue;
            WTPrincipal wtprincipal = (WTPrincipal) persistable;
            if (wtprincipal == null)
            {
                continue;
            }
            String oid = PersistenceHelper.getObjectIdentifier(wtprincipal).toString();
            if (VERBOSE)
            {
                System.out.println(" WorkFlowStepCommand addPrincipalsToParticipantSelect  wtprincipal=" + wtprincipal.getDisplayIdentifier());
                System.out.println(" WorkFlowStepCommand addPrincipalsToParticipantSelect  wtprincipal oid=" + oid);
            }
            if (tableitem.indexOf(oid) == -1)
            {
                if (VERBOSE)
                    System.out.println(" WorkFlowStepCommand addPrincipalsToParticipantSelect  add to addDynamicRefreshInfo=" + wtprincipal.getDisplayIdentifier());
                formresult = addDynamicRefreshInfo(formresult, wtprincipal, "A");
            }
        }
        return formresult;
    }

    public static FormResult removePrincipalsFromTable(NmCommandBean nmcommandbean) throws WTException
    {
        NmOid nmoid = nmcommandbean.getElementOid();
        FormResult formresult = new FormResult(FormProcessingStatus.SUCCESS);
        formresult.setNextAction(FormResultAction.NONE);
        ArrayList arraylist = nmcommandbean.getSelected();
        if (arraylist != null)
        {
            for (int i = 0; i < arraylist.size(); i++)
            {
                NmContext nmcontext = (NmContext) arraylist.get(i);
                Persistable persistable = nmcontext.getTargetOid().getWtRef().getObject();
                if (!(persistable instanceof WTPrincipal))
                    continue;
                WTPrincipal wtprincipal = (WTPrincipal) persistable;
                formresult = addDynamicRefreshInfo(formresult, wtprincipal, "Del");
            }
        }
        return formresult;
    }

    protected static FormResult addDynamicRefreshInfo(FormResult formresult, Persistable persistable, String s) throws WTException
    {
        formresult.addDynamicRefreshInfo(new DynamicRefreshInfo(persistable, persistable, s));
        return formresult;
    }

    protected static ArrayList retrievePrincipalArrayFromPickerPrincipals(NmCommandBean nmcommandbean, String s)
    {
        ArrayList arraylist = new ArrayList();
        int i = 0;
        for (int j = s.indexOf("#", i); j != -1; j = s.indexOf("#", i))
        {
            String s1 = s.substring(i, j);
            arraylist.add(s1);
            i = j + 1;
        }

        return arraylist;
    }

    protected static WTPrincipal getPrincipal(String s) throws WTException
    {
        boolean flag = false;
        int i = s.indexOf(":");
        if (i > -1)
        {
            String s1 = s.substring(0, i);
            if (WTUser.class.getName().equals(s1) || WTGroup.class.getName().equals(s1) || WTOrganization.class.getName().equals(s1))
            {
                flag = true;
            }
        }
        WTPrincipal wtprincipal = null;
        if (flag)
        {
            ObjectIdentifier objectidentifier = new ObjectIdentifier(s);
            wtprincipal = getPrincipal(objectidentifier);
        }
        else
        {
            wtprincipal = getPrincipalByDN(s, null, true);
        }
        return wtprincipal;
    }

    protected static WTPrincipal getPrincipal(ObjectIdentifier objectidentifier) throws WTException
    {
        return OrganizationServicesHelper.manager.getPrincipal(objectidentifier);
    }

    protected static WTPrincipal getPrincipalByDN(String s, String s1, boolean flag) throws WTException
    {
        return OrganizationServicesHelper.manager.getPrincipalByDN(s, s1, flag);
    }
}
